package com.cspg.design.ui.setting;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.cspg.design.R;
import com.cspg.design.presenter.LoginPresenter;
import com.cspg.design.ui.login.LoginActivity;
import com.cspg.design.utils.ActionBarUtil;
import com.cspg.design.utils.StatusBarUtil;
import com.cspg.design.widget.ToastUtils;

public class SecurityActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_security);
        StatusBarUtil.setStatusBarColor(this, R.color.colorPrimary);
        ActionBarUtil.setActionBar(this, R.string.update_password);
        EditText etOldPassword = findViewById(R.id.et_old_pwd);
        EditText etNewPassword = findViewById(R.id.et_new_pwd);
        EditText etNewPasswordRepeat = findViewById(R.id.et_new_pwd_repeat);
        Button btnSave = findViewById(R.id.btn_pwd_save);
        LoginPresenter loginPresenter = new LoginPresenter(this);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(SecurityActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                String newPwd= etNewPassword.getText().toString();
                String newPwdRe = etNewPasswordRepeat.getText().toString();
                String oldPwd = etOldPassword.getText().toString();
                if (newPwd.equals(newPwdRe))
                {
                    loginPresenter.upatePassword(oldPwd,newPwd);
                }
                else
                {
                    ToastUtils.toast(SecurityActivity.this,"新密码输入不一致......");
                }
            }
        });

//        ConstraintLayout clPwd = findViewById(R.id.cl_pwd);
//        clPwd.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                imm.hideSoftInputFromWindow(SecurityActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//            }
//        });
    }
    //点击空白处收起键盘
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            if (SecurityActivity.this.getCurrentFocus().getWindowToken() != null) {

                imm.hideSoftInputFromWindow(SecurityActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

            }

        }

        return super.onTouchEvent(event);

    }
}